<template>
  <div class="abnormal-device">
    <div>
      <label>{{ $t("route.Maintenance_Management") }}</label>
      <div></div>
      <button>
        <img /><label>{{ $t("route.Maintenance_management") }}</label>
      </button>
    </div>

    <div>
      <div>
        <el-select
          v-model="selectValue1"
          @change="selectChange1"
          :placeholder="$t('route.Please_Select')"
        >
          <el-option
            v-for="(item, index) in workshopStoreList"
            :label="item.name"
            :value="index"
          >
          </el-option>
        </el-select>
        <el-select
          @change="selectChange2"
          style="margin-left: 10px"
          v-model="query.kind"
          :placeholder="$t('route.Please_select_the_type')"
        >
          <el-option v-for="item in kindList" :key="item" :label="item" :value="item">
          </el-option>
        </el-select>

        <div class="div-handle-right">
          <el-button @click="searchClick">{{ $t("route.search") }}</el-button>
          <input v-model="query.search" :placeholder="$t('route.Head_Model')" />
        </div>
      </div>

      <el-table class="table" :data="deviceList" style="width: 100%">
        <el-table-column prop="online" :label="$t('route.state')" width="80">
          <template #default="scope">
            <div :style="{ color: scope.row.online ? '#0f0' : '#888' }">
              {{ scope.row.online ? $t("route.Startup") : $t("route.Shutdown") }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="kind" :label="$t('route.kind')" />
        <el-table-column prop="noseCode" :label="$t('route.Head_Model')" />
        <el-table-column :label="$t('route.location')" width="130">
          <template #default="scope">
            {{ scope.row.workshop ? scope.row.workshop : scope.row.store }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('route.Total_sewing_time')">
          <template #default="scope">
            {{ formatSewDur(scope.row) }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('route.Remaining_maintenance_sewing_time')">
          <template #default="scope">
            {{ surplusDur(scope.row) }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('route.Last_maintenance_time')">
          <template #default="scope">
            {{
              scope.row.maintainTime
                ? new Date(scope.row.maintainTime).format("yyyy-MM-dd")
                : "---"
            }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('route.Maintenance_content')">
          <template #default="scope">
            {{ $t("route.fuel_charging") }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('route.Tips')">
          <template #default="scope">
            <div class="div-handle">
              <img style="width: 35px; height: 35px" :src="formatImg(scope.row)" />
              <el-button
                style="color: #669900"
                @click="maintainClick(scope.row.id)"
                type="text"
                >{{ $t("route.Maintenance_completed") }}
              </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          style="float: right"
          background
          @current-change="handlePageChange"
          :current-page="query.pageNum"
          :page-size="query.pageSize"
          layout="total, prev, pager, next"
          :total="query.total"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import http from "../../js/http/http.js";
import api from "../../js/http/api.js";
import CommonUtils from "../../js/common/common-utils.js";
import Constant from "../../js/common/constant.js";
import i18n from "../../i18n/index";
export default {
  data() {
    return {
      query: {
        kind: i18n.global.t("route.All_categories"),
        pageNum: 1,
        pageSize: 10,
        total: 0,
        search: null,
      },
      deviceList: [],
      kindList: [i18n.global.t("route.All_categories"), ...Constant.DEVICE_KIND],
      workshopStoreList: [],
      selectIndex1: 0,
      selectValue1: i18n.global.t("route.All"),
    };
  },
  methods: {
    formatSewDur(item) {
      if (item.dataConf) {
        var dataConf = JSON.parse(item.dataConf);
        if (dataConf.sewDur == undefined || dataConf.todaySewDur == undefined)
          return "---";
        return CommonUtils.formatDur(dataConf.sewDur + dataConf.todaySewDur);
      } else {
        return "---";
      }
    },

    formatImg(item) {
      var dur = 720000;
      if (item.dataConf) {
        var dataConf = JSON.parse(item.dataConf);
        if (item.sewDur) {
          dur = 720000 - (dataConf.sewDur + dataConf.todaySewDur - item.sewDur);
        } else {
          dur = 720000 - (dataConf.sewDur + dataConf.todaySewDur);
        }
      }
      return dur <= 0
        ? require("../../assets/img/icon-9.png")
        : require("../../assets/img/icon-10.png");
    },

    surplusDur(item) {
      var dur = 720000;
      if (item.dataConf) {
        var dataConf = JSON.parse(item.dataConf);
        if (dataConf.sewDur == undefined || dataConf.todaySewDur == undefined)
          return "---";
        if (item.sewDur) {
          dur = 720000 - (dataConf.sewDur + dataConf.todaySewDur - item.sewDur);
        } else {
          dur = 720000 - (dataConf.sewDur + dataConf.todaySewDur);
        }
      }
      return this.formatDur(dur);
    },
    formatDur(dur) {
      return CommonUtils.formatDur(dur);
    },
    jumpPage(type, param1) {
      if (type == 0) {
        this.$router.push({
          name: "repair-record",
          params: {
            noseCode: param1.noseCode,
          },
        });
      }
    },
    selectChange1(value) {
      this.query.search = null;
      this.query.pageNum = 1;
      this.selectIndex1 = value;
      this.getMaintainDevice();
    },
    selectChange2() {
      this.query.search = null;
      this.query.pageNum = 1;
      this.getMaintainDevice();
    },

    handlePageChange(pageNum) {
      this.query.pageNum = pageNum;
      this.getMaintainDevice();
    },
    searchClick() {
      this.query.pageNum = 1;
      this.getMaintainDevice();
    },
    getWorkshopStore() {
      http.post(
        api.GET_WORKSHOP_STORE,
        {
          adminId: this.user.id,
          factory: Constant.FACTORY == "全部工厂" ? null : Constant.FACTORY,

        },
        (data) => {
          this.workshopStoreList = data.data;
          this.workshopStoreList.splice(0, 0, {
            type: 0,
            name: i18n.global.t("route.All"),
          });
        }
      );
    },
    getMaintainDevice() {
      var reqType = 0;
      var search = null;
      var inType = null;
      if (this.selectIndex1 != 0) {
        var obj = this.workshopStoreList[this.selectIndex1];
        reqType = obj.type;
        inType = obj.name;
      }
      var params = {
        adminId: this.user.id,
        reqType: reqType,
        inType: inType,
        factory: Constant.FACTORY == "全部工厂" ? null : Constant.FACTORY,
        kind:
          this.query.kind == i18n.global.t("route.All_categories")
            ? null
            : this.query.kind,
        search: this.query.search,
        pageNum: this.query.pageNum,
        pageSize: this.query.pageSize,
      };
      http.post(api.GET_MAINTAIN_DEVICE, params, (data) => {
        this.deviceList = data.data.deviceList;
        this.query.total = data.data.total;
      });
    },
    formatState(state) {
      //0:没有异常 1:普通维修 2:大修 3报废 4:异常报警
      if (state == 0) {
        return i18n.global.t("route.normal");
      } else if (state == 1) {
        return i18n.global.t("route.General_maintenance");
      } else if (state == 2) {
        return i18n.global.t("route.Overhaul");
      } else if (state == 3) {
        return i18n.global.t("route.Scrap");
      } else if (state == 4) {
        return i18n.global.t("route.Abnormal_alarm");
      }
    },
    maintainClick(deviceId) {
      this.$confirm(i18n.global.t("route.continue9"), i18n.global.t("route.reminder"), {
        confirmButtonText: i18n.global.t("route.sure"),
        cancelButtonText: i18n.global.t("route.cancel"),
        type: "warning",
      }).then(() => {
        http.post(
          api.ADD_MAINTAIN,
          {
            deviceId: deviceId,
          },
          (data) => {
            this.query.pageNum = 1;
            this.getMaintainDevice();
          }
        );
      });
    },

    getAllKind() {
      http.post(
        api.GET_ALL_KIND,
        {
          adminId: this.$store.state.user.id,
        },
        (data) => {
          this.kindList = [i18n.global.t("route.All_categories"), ...data.data];
        }
      );
    },
  },
  created() {
    this.getAllKind();
    this.user = this.$store.state.user;
    this.getWorkshopStore();
    this.getMaintainDevice();
  },

  mounted() {},
};
</script>
<style lang="scss" scoped>
.div-handle {
  display: flex;
  flex-direction: row;
  align-items: center;

  > img {
    margin-right: 10px;
  }
}

.abnormal-device {
  color: #444444;
  font-size: 18px;
  padding-right: 20px;
  height: calc(100vh - 100px);
  // overflow: scroll;
  scrollbar-width: none;
  /* firefox */
  -ms-overflow-style: none;
  /* IE 10+ */
  overflow-x: hidden;
  overflow-y: auto;

  > div:nth-of-type(1) {
    display: flex;
    flex-direction: row;
    align-items: center;
    margin-top: 20px;
    background-color: #ffffff;
    padding: 20px;
    border-radius: 5px;

    > label:nth-of-type(1) {
      font-weight: 600;
    }

    > div:nth-of-type(1) {
      width: 1px;
      background-color: #cccccc;
      height: 15px;
      margin: 0px 20px;
    }

    > button:nth-of-type(1) {
      background-image: url(../../assets/img/customer-1.png);
      background-repeat: no-repeat;
      background-size: 100% 100%;
      padding: 5px 10px;
      border: 0px;
      display: flex;
      color: #ffffff;
      flex-direction: row;
      align-items: center;
      font-weight: 800;

      > img {
        margin-right: 7px;
        width: 15px;
        height: 15px;
        content: url(../../assets/img/customer-2.png);
      }
    }
  }

  > div:nth-of-type(2) {
    display: flex;
    flex-direction: column;
    margin-top: 20px;
    background-color: #ffffff;
    padding: 20px;
    border-radius: 5px;

    > div:nth-of-type(1) {
      display: flex;
      flex-direction: row;
      align-items: center;

      .div-handle-right {
        flex: 1;
        flex-direction: row-reverse;
        text-align: right;
        display: flex;
        align-items: center;

        > input:nth-of-type(1) {
          border: 1px solid #eeeeee;
          font-size: 15px;
          width: 200px;
          padding: 5px 8px;
          outline: none;
        }

        > button {
          color: #a866ff;
          margin-left: 10px;
          padding: 4px 20px;
        }
      }
    }

    .table {
      margin-top: 20px;
    }
  }

  .select {
    width: 120px;
    margin-top: 10px;
  }

  .echart {
    height: 150px;
    width: 100%;
  }

  .input {
    width: 250px;
  }
}
</style>
